package com.example.demo.controller;
import com.alibaba.fastjson.JSON;
import com.example.demo.dao.PersonRepository;
import com.example.demo.dao.productRepository;
import com.example.demo.model.WeichuListResponse;
import com.example.demo.model.userTable;
import com.example.demo.model.productTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
/**
 * Created by zouwenchang on 2018/1/26.
 */
@RestController
public class userController {
    @Autowired
    PersonRepository personRepository;
    @Autowired
    productRepository productRepository;

    @CrossOrigin(origins = "*", allowedHeaders = "*", allowCredentials = "true")
    @GetMapping(value = "/person")
    private String personList() {
        WeichuListResponse weichuRsp=new WeichuListResponse();
        weichuRsp.setMsg("success");
        weichuRsp.setCode("0000");
        weichuRsp.setItems((ArrayList) personRepository.findAll());
        return JSON.toJSONString(weichuRsp);
    }
    @CrossOrigin(origins = "*", allowedHeaders = "*", allowCredentials = "true")
    @PostMapping(value = "/addPerson")
    private userTable addPerson(@RequestParam("id") Integer id,
                                @RequestParam("email") String email,
                                @RequestParam("nick") String nick,
                                @RequestParam("pwd") String pwd,
                                @RequestParam("names") String names
                                ) {
        userTable users=new userTable();

        users.setEmail(email);
        users.setId(id);
        users.setNickName(nick);
        users.setUserName(names);
        users.setPassword(pwd);

        return personRepository.save(users);

    }
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    @PostMapping(value = "/addProduct")
    private productTable addProduct(HttpServletRequest request, HttpServletResponse response){
        productTable pros=new productTable();


        return productRepository.save(pros);
    }
}
